﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class ModifyPwd : System.Web.UI.PageEx
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string strResult = string.Empty;
            if (!DalEx.DbUserDalEx.CheckUserLogin(UserType, UserId,ref strResult))
            {
                MessageBox(strResult, "UserLogin.aspx");
                return;
            }
            string strLink = "<li><a href=\"{0}\" class=\"{2}\">{1}</a></li>";
            StringBuilder sbMenuLinks = new StringBuilder();
            if (UserType == 1 && UserId > 0)
            {
                sbMenuLinks.Append(string.Format(strLink, new object[] { "MemberCenter.aspx", "会员中心", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "UserInfos.aspx", "我的资料", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "ModifyPwd.aspx", "修改密码", "on" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "MyBaoMing.aspx", "我的报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "MyYiGongBaoMing.aspx", "义工报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "MyHomeEventBaoMing.aspx", "家园活动报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "Logout.aspx", "退出系统", "" }));
            }
            else
            {
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "会员中心", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "我的资料", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "修改密码", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "我的报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "义工报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "家园活动报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "退出系统", "" }));
            }
            litMenuLinks.Text = sbMenuLinks.ToString();

            DataBinds();
        }
    }
    private void DataBinds()
    {
        if (UserType == 1 && UserId > 0)
        {
            Model.DbUsers dbUser = DalEx.DbUserDalEx.Select(UserId);
            if (dbUser != null && !string.IsNullOrEmpty(dbUser.Tel) && !string.IsNullOrEmpty(dbUser.Email))
            {
                hfUserId.Value = dbUser.UserID.ToString();
                txtUserName.Text = dbUser.UserName;
            }
            else
            {
                MessageBox("请选择完善您的注册资料！", "UserInfos.aspx");
                return;
            }
        }
    }
    protected void btnModify_Click(object sender, EventArgs e)
    {
        int iUserId = 0;
        int.TryParse(hfUserId.Value, out iUserId);
        string strOldPwd = SqlHelper.String(txtOldPwd.Text);
        string strNewPwd = SqlHelper.String(txtUserPwd.Text);

        string strResult = string.Empty;

        string strConfirmPwd = SqlHelper.String(txtConfirmPwd.Text);
        if (!strNewPwd.Equals(strConfirmPwd))
        {
            MessageBox("抱歉，您两次输入的密码不一致！");
            return;
        }
        Model.DbUsers dbUser = DalEx.DbUserDalEx.Select(iUserId);
        if (dbUser != null)
        {
            if (dbUser.UserPwd == SqlHelper.MD5(strOldPwd, 32))
            {
                dbUser.UserPwd = SqlHelper.MD5(strNewPwd, 32);
                if (DalEx.DbUserDalEx.ChangePwd(dbUser, ref strResult))
                {
                    MessageBox(strResult,"MemberCenter.aspx");
                }
                else
                {
                    MessageBox(strResult);
                    return;
                }
            }
            else
            {
                MessageBox("抱歉，您输入的旧密码有误！");
            }
        }
        else
        {
            MessageBox("抱歉，您尚未登录或登录已超时！");
            return;
        }
    }
}
